package com.lmpop.frame.util;

import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

public class PageUtil
{
  int disNum = 10;
  String startName = "start";
  String indexCss;
  String upCss;
  String downCss;
  String lastCss;
  String pageCss;
  String numCss;
  String thisNumCss;
  String indexName = "首 页";
  String upName = "上一页";
  String downName = "下一页";
  String lastName = "尾 页";

  public PageUtil() {
    this.indexCss = "[index/]";
    this.upCss = "[uppage/]";
    this.downCss = "[downpage/]";
    this.lastCss = "[lastpage/]";
    this.pageCss = "[current/]/[total/]";
    this.numCss = "[[num/]]";
    this.thisNumCss = "<font color='red'>[[thisnum/]]</font>";
  }

  public PageUtil(String indexCss, String upCss, String downCss, String lastCss, String pageCss, String numCss, String thisNumCss)
  {
    this.indexCss = indexCss;
    this.upCss = upCss;
    this.downCss = downCss;
    this.lastCss = lastCss;
    this.pageCss = pageCss;
    this.numCss = numCss;
    this.thisNumCss = thisNumCss;
  }

  public String getPageString(String url, int count, int limit, int current, HttpServletRequest req) {
    if (limit == 0) return "";
    String urlhref = "";
    if (req != null)
      urlhref = urlString(req, url);
    else {
      urlhref = url;
    }

    int pageNum = (count - 1) / limit + 1;
    if (current > pageNum) current = pageNum;
    if (current < 0) current = 0;
    int disHalf = this.disNum / 2;
    int start = 0; int end = 0;

    if (current - disHalf <= 0) {
      start = 1;
      end = current + (this.disNum - current);
    } else {
      start = current - disHalf;
      if (current + disHalf > pageNum)
        end = pageNum;
      else {
        end = current + disHalf;
      }
      if (end == pageNum) {
        start = pageNum - this.disNum;
      }
    }

    StringBuffer sb = new StringBuffer();
    if (pageNum > 1) {
      if (pageNum <= this.disNum) {
        start = 1;
        end = pageNum;
      }
      if (current != 1) {
        if (!this.indexCss.equals("")) sb.append(createHref(this.indexCss, 1, urlhref, this.indexName, "[index/]"));
        if (!this.upCss.equals("")) sb.append(createHref(this.upCss, current - 1, urlhref, this.upName, "[uppage/]"));
      }
      for (int i = start; i <= end; i++) {
        sb.append(createHref(current, i, urlhref));
      }
      if (current != pageNum) {
        if (!this.downCss.equals("")) sb.append(createHref(this.downCss, current + 1, urlhref, this.downName, "[downpage/]"));
        if (!this.lastCss.equals("")) sb.append(createHref(this.lastCss, pageNum, urlhref, this.lastName, "[lastpage/]"));
      }
    }

    sb.append(this.pageCss.replace("[current/]", String.valueOf(current)).replace("[total/]", String.valueOf(pageNum)));
    return sb.toString();
  }

  public String getPageString(String url, int count, int limit, int current, HttpServletRequest req, String style)
  {
    if (limit == 0) return "";
    String urlhref = "";
    if (req != null)
      urlhref = urlString(req, url);
    else {
      urlhref = url;
    }

    int pageNum = (count - 1) / limit + 1;
    if (current > pageNum) current = pageNum;
    if (current < 0) current = 0;
    int disHalf = this.disNum / 2;
    int start = 0; int end = 0;

    if (current - disHalf <= 0) {
      start = 1;
      end = current + (this.disNum - current);
    } else {
      start = current - disHalf;
      if (current + disHalf > pageNum)
        end = pageNum;
      else {
        end = current + disHalf;
      }
      if (end == pageNum) {
        start = pageNum - this.disNum;
      }
    }

    StringBuffer sb = new StringBuffer();
    if (pageNum > 1) {
      if (pageNum <= this.disNum) {
        start = 1;
        end = pageNum;
      }
      if (current != 1) {
        if ((style != null) && (!"".equals(style))) {
          if (!this.indexCss.equals("")) sb.append(createHref(this.indexCss, 1, urlhref, this.indexName, "[index/]", style));
          if (!this.upCss.equals("")) sb.append(createHref(this.upCss, current - 1, urlhref, this.upName, "[uppage/]", style)); 
        }
        else {
          if (!this.indexCss.equals("")) sb.append(createHref(this.indexCss, 1, urlhref, this.indexName, "[index/]"));
          if (!this.upCss.equals("")) sb.append(createHref(this.upCss, current - 1, urlhref, this.upName, "[uppage/]"));
        }
      }

      if ((style != null) && (!"".equals(style))) {
        for (int i = start; i <= end; i++)
          sb.append(createHref(current, i, urlhref, style));
      }
      else {
        for (int i = start; i <= end; i++) {
          sb.append(createHref(current, i, urlhref));
        }
      }

      if (current != pageNum) {
        if ((style != null) && (!"".equals(style))) {
          if (!this.downCss.equals("")) sb.append(createHref(this.downCss, current + 1, urlhref, this.downName, "[downpage/]", style));
          if (!this.lastCss.equals("")) sb.append(createHref(this.lastCss, pageNum, urlhref, this.lastName, "[lastpage/]", style)); 
        }
        else {
          if (!this.downCss.equals("")) sb.append(createHref(this.downCss, current + 1, urlhref, this.downName, "[downpage/]"));
          if (!this.lastCss.equals("")) sb.append(createHref(this.lastCss, pageNum, urlhref, this.lastName, "[lastpage/]"));
        }
      }

    }

    sb.append(this.pageCss.replace("[current/]", String.valueOf(current)).replace("[total/]", String.valueOf(pageNum)));
    return sb.toString();
  }

  public static void main(String[] args)
  {
    String ss = "supply.jsp?ddd=1&fff=3&";
    int i = ss.lastIndexOf("&");
    System.out.println(ss.substring(i, ss.length()));
  }

  public String createHref(int current, int i, String url) {
    url = urlSymbol(url, i);
    String numCssObj = "";
    String iurl = "<a href='" + url + "'>" + String.valueOf(i) + "</a>";
    if (current == i)
      numCssObj = this.thisNumCss.replace("[thisnum/]", iurl);
    else {
      numCssObj = this.numCss.replace("[num/]", iurl);
    }
    return numCssObj;
  }
  public String createHref(int current, int i, String url, String style) {
    url = urlSymbol(url, i);
    String numCssObj = "";
    String iurl = "<a style='" + style + "' href='" + url + "'>" + String.valueOf(i) + "</a>";
    if (current == i)
      numCssObj = this.thisNumCss.replace("[thisnum/]", iurl);
    else {
      numCssObj = this.numCss.replace("[num/]", "<a href='" + url + "'>" + String.valueOf(i) + "</a>");
    }
    return numCssObj;
  }
  public String createHref(String tempStr, int i, String url, String reStr, String codeStr) {
    url = urlSymbol(url, i);
    tempStr = tempStr.replace(codeStr, "<a href='" + url + "'>" + reStr + "</a>");
    return tempStr;
  }

  public String createHref(String tempStr, int i, String url, String reStr, String codeStr, String style) {
    url = urlSymbol(url, i);
    tempStr = tempStr.replace(codeStr, "<a  href='" + url + "'>" + reStr + "</a>");
    return tempStr;
  }

  public String urlSymbol(String url, int i)
  {
    Pattern p = Pattern.compile(this.startName + "=[0-9]+");
    Matcher m = p.matcher(url);
    String startStr = "";
    while (m.find()) {
      startStr = m.group();
    }
    if (!startStr.equals(""))
      url = url.replace(startStr, this.startName + "=" + i);
    else {
      url = url + "&" + this.startName + "=" + i;
    }
    return url;
  }

  public String urlString(HttpServletRequest req, String url)
  {
    if (req == null) return url;
    Enumeration reqParamNames = req.getParameterNames();
    StringBuffer sb = new StringBuffer();
    sb.append(url + "?");
    while (reqParamNames.hasMoreElements()) {
      String fieldName = (String)reqParamNames.nextElement();
      String fieldValue = "";
      if (req.getParameter(fieldName) != null) {
        fieldValue = req.getParameter(fieldName);
      }
      if (!fieldValue.trim().equals("")) sb.append(fieldName + "=" + fieldValue + "&");
    }
    String sbStr = sb.toString();
    if (sbStr.indexOf("&") > -1) {
      int i = sbStr.lastIndexOf("&");
      sbStr = sbStr.substring(0, i);
    }
    try {
      sbStr = new String(sbStr.getBytes("UTF-8"), "ISO-8859-1");
    } catch (Exception e) {
      e.printStackTrace();
    }
    return sbStr;
  }
}